<?php
/*
* PHP Template created by Nicky
*/
include_once 'AdminController.php';

class phb extends AdminController {
	
	var $pageSize = 3;
	
	function _construct(){
		parent::__construct();
		if(empty($_SESSION['LOGIN'])){
		 	echo '<script>window.top.location = "/admin.php?c=main&a=login";</script>';
		 	exit();
		 } 
	}
	
	function index(){  
		$limtestr = '0,20';
		$gModel = spClass('m_wx_game');   
		$datalist = $gModel->findAll(array('in_phd'=>2),'money desc','*',$limtestr);
		$data = array(); 
		foreach($datalist as $od){
			$tarr = $od; 
			$us = $this->getUser($od['uid']);
			$tarr['nickname'] = base64_decode($us['nickname']);
			$tarr['headimg'] = $us['headimgurl'];
			$data[] = $tarr ; 
		}
		$this->datalist = $data;  
	}
	
	function bangdan(){ 
		$glModel = spClass('m_wx_gamelist');
		$datalist = $glModel->findAll(null,'orderindex');
		$data = array(); 
		foreach($datalist as $od){
			$tarr = $od; 
			$us = $this->getUser($od['openid']);
			$tarr['nickname'] = base64_decode($us['nickname']);
			$tarr['headimg'] = $us['headimgurl'];
			$data[] = $tarr ; 
		}
		$this->datalist = $data; 
		//$this->_D($data);
	}
	
	function setInbd(){
		$glModel = spClass('m_wx_gamelist');
		$gModel = spClass('m_wx_game');
		$uModel = spClass('m_wx_user');
		$gid = $this->spArgs('gid',0);
		if($gid<1){
			$this->_Ajax_return(false,'Parm Error');
		}
		//获取用户信息
		$gm = $gModel->find(array('id'=>$gid));
		if(empty($gm)){
			$this->_Ajax_return(false,'Parm Error');
		}
		$openid = $gm['uid'];
		$user = $uModel->find(array('openid'=>$openid));
		if(empty($user)){
			$this->_Ajax_return(false,'User Empty');
		}
		$gl = $glModel->find(null,'id desc');
		$orderindex = 1;
		if(!empty($gl)){
			$orderindex = $gl['id']+1;
		}
		$data = array(
			'openid'	=>	$openid,
			'nickname'	=>	$user['nickname'],
			'headimg'	=>	$user['headimg'],
			'money'	=>	$gm['money'],
			'orderindex'	=>	$orderindex, 
		);
		$glModel->create($data);
		$gModel->update(array('id'=>$gid),array('in_phd'=>1));
		$this->_Ajax_return(true,'','');
	}
	
	function getUser($openid){
		$uModel = spClass('m_wx_user');
		return $uModel->find(array('openid'=>$openid));
	}
}